package database.team.phase2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import java.io.PrintWriter;

public class BagDifference {

	public BagDifference() throws IOException{
			String valuePKE1="",tupleContent,valuePKE2 = "";
			int empIDPK2=0,empIDPK1 = 0,empIDPK1Temp = 0, counterPKE1=1, endbag =0,endbag2 =0;
			boolean bag = false;
			File fileEmpl1 = new File("FileSortedAndMerged_TableEmp1.txt");
			File fileEmpl2 = new File("FileSortedAndMerged_TableEmp2.txt");
			FileWriter output = new FileWriter("BagDifference.txt");
			PrintWriter tuple = new PrintWriter(output);
			
			long sizeoffileEmpl1 = fileEmpl1.length();
	        long blocksizeEmpl1 = sizeoffileEmpl1 / 1024 + (sizeoffileEmpl1 % 1024 == 0 ? 0 : 1);
	        long freemem = Runtime.getRuntime().freeMemory();
	        if (blocksizeEmpl1 < freemem / 2) {
	                blocksizeEmpl1 = freemem/4;
	        }
	        
	        long sizeoffileEmpl2 = fileEmpl2.length();
	        long blocksizeEmpl2 = sizeoffileEmpl2 / 1024 + (sizeoffileEmpl2 % 1024 == 0 ? 0 : 1);
	        long freemem1 = Runtime.getRuntime().freeMemory();
	        if (blocksizeEmpl2 < freemem1 / 2) {
	                blocksizeEmpl2 = freemem1/4;
	        }
	   		
			//Perform a bag difference between table Employee1 as a relation R and Employee2 as a relation S
	           
			    BufferedReader Empl1 = new BufferedReader(new FileReader(fileEmpl1));
				valuePKE1 = Empl1.readLine();
				if(valuePKE1 != null){
					empIDPK1 = Integer.parseInt(valuePKE1.substring(0, 7));
				}
				BufferedReader Empl2 = new BufferedReader(new FileReader(fileEmpl2));
				valuePKE2 = Empl2.readLine();
				if(valuePKE2 != null){
					empIDPK2 = Integer.parseInt(valuePKE2.substring(0, 7));
				}
				String tuplevalue="";
				while(empIDPK2<empIDPK1 && valuePKE2 != null){
					valuePKE2 = Empl2.readLine();
					if(valuePKE2 != null){
						empIDPK2 = Integer.parseInt(valuePKE2.substring(0, 7));
					}
				}
				int tuplecounter=0;
				   while (valuePKE1 != null || endbag == 1){
						if(tuplecounter ==0){
							  tuplevalue = valuePKE1;
							  valuePKE1 = Empl1.readLine();
							  tuplecounter++; 
						}
						else{
							   valuePKE1 = Empl1.readLine();
						}
						if(valuePKE1 != null){
							empIDPK1Temp = Integer.parseInt(valuePKE1.substring(0, 7));
						}
						else{
							endbag++;
							empIDPK1Temp = 0;
							if(valuePKE2 == null){endbag2=1;}
						}
					    if(empIDPK1Temp == empIDPK1){
							   counterPKE1++;
						} 
					   else{
						  
							while (empIDPK1Temp!=empIDPK1){// && valuePKE2!=null || endbag2 == 1){
								bag = true;
								if(empIDPK1 == empIDPK2){
									counterPKE1--;	
									valuePKE2 = Empl2.readLine();
									if(valuePKE2 != null){
										empIDPK2 = Integer.parseInt(valuePKE2.substring(0, 7));
									}
									else{
										endbag2++;
										empIDPK2 = 0;
									}
								}
								else{
									if (counterPKE1 > 0 && bag == true){
										for(int i=0; i<counterPKE1;i++){
											tupleContent =tuplevalue;	
											tuple.println(tupleContent);
										}
									}
									
									counterPKE1 = 1;
									empIDPK1 = empIDPK1Temp;
									tuplecounter=0;
									bag = false;
									endbag =0;
									endbag2 =0;
									while(empIDPK2< empIDPK1Temp && valuePKE2!=null){
										valuePKE2 = Empl2.readLine();
										if(valuePKE2 != null){
											empIDPK2 = Integer.parseInt(valuePKE2.substring(0, 7));
										}
									}
								}
							}
					   }	   
			    	}
				   tuple.close(); output.close();
	}

					
/*	 public static void main(String[] args) throws IOException {
	 System.out.println("Bag Difference between the tables Employee1 and Employee2");
	 long startTime = System.currentTimeMillis();
	 new BagDifference();
	 long endTime = System.currentTimeMillis();
	 float totalTime = (endTime - startTime);
	// System.out.println("Total Disk I/O for bag difference: "+(totalIOJoin)); 
	 System.out.println("Total time to perform the BagDifference between tables Employee1 and Employee2: "+totalTime); 
	 System.out.println("The result is shown in the file BagDifference.txt");
}*/

}


